package cn.iocoder.yudao.module.cmt.controller.admin.content;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.content.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.content.ContentLikeDO;
import cn.iocoder.yudao.module.cmt.service.content.ContentLikeService;

@Tag(name = "管理后台 - 点赞")
@RestController
@RequestMapping("/cmt/content-like")
@Validated
public class ContentLikeController {

    @Resource
    private ContentLikeService likeService;

    @PostMapping("/create")
    @Operation(summary = "创建点赞")
    @PreAuthorize("@ss.hasPermission('cmt:content-like:create')")
    public CommonResult<Long> createLike(@Valid @RequestBody ContentLikeSaveReqVO createReqVO) {
        return success(likeService.createLike(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新点赞")
    @PreAuthorize("@ss.hasPermission('cmt:content-like:update')")
    public CommonResult<Boolean> updateLike(@Valid @RequestBody ContentLikeSaveReqVO updateReqVO) {
        likeService.updateLike(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除点赞")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:content-like:delete')")
    public CommonResult<Boolean> deleteLike(@RequestParam("id") Long id) {
        likeService.deleteLike(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除点赞")
    @PreAuthorize("@ss.hasPermission('cmt:content-like:delete')")
    public CommonResult<Boolean> deleteLikeList(@RequestParam("ids") List<Long> ids) {
        likeService.deleteLikeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得点赞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:content-like:query')")
    public CommonResult<ContentLikeRespVO> getLike(@RequestParam("id") Long id) {
        ContentLikeDO like = likeService.getLike(id);
        return success(BeanUtils.toBean(like, ContentLikeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得点赞分页")
    @PreAuthorize("@ss.hasPermission('cmt:content-like:query')")
    public CommonResult<PageResult<ContentLikeRespVO>> getLikePage(@Valid ContentLikePageReqVO pageReqVO) {
        PageResult<ContentLikeDO> pageResult = likeService.getLikePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContentLikeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出点赞 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:content-like:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLikeExcel(@Valid ContentLikePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContentLikeDO> list = likeService.getLikePage(pageReqVO).getList();
        ExcelUtils.write(response, "点赞.xls", "数据", ContentLikeRespVO.class,
                        BeanUtils.toBean(list, ContentLikeRespVO.class));
    }

}
